package com.youdianstar.app.common.widget

import android.app.Activity
import android.content.Context
import android.util.AttributeSet
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import androidx.core.content.ContextCompat
import androidx.databinding.DataBindingUtil
import com.youdianstar.app.common.R
import com.youdianstar.app.common.databinding.LayoutHeadBarBinding
import com.gyf.immersionbar.ImmersionBar
import com.youdianstar.app.common.ex.getViewColor



/**
 * File: HeadBar
 * Author: 82149 Create: 2021/10/8 17:11
 * Changes (from 2021/10/8)
 * --------------------------------------------------
 * description:
 * ---------------------------------------------------
 *
 */
class HeadBar @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr){
    private var headBarBinding :LayoutHeadBarBinding? =null

    init {
        headBarBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.layout_head_bar,null,false)
        headBarBinding?.let {
            addView(it.root,0)
        }
    }

    /**
     * HeadBar
     * ---默认背景, 返回箭头黑色/背景白色
     * @param act 页面
     * @param title 标题文本
     * @param isOPBar 是否编辑Status bar
     */
    fun initTitle(act : Activity, title : String, isOPBar :Boolean = true, statusColor:Int = 0,
                  showInner :Boolean = false, isBackFinish :Boolean = true, backAction: () -> Unit = {}){
        if (isOPBar){
            if (statusColor!=0){
                ImmersionBar.with(act).statusBarDarkFont(false)
                    .statusBarColor(statusColor).titleBarMarginTop(this).init()
            }else{
                ImmersionBar.with(act).statusBarDarkFont(true)
                    .statusBarColor(R.color.white).titleBarMarginTop(this).init()
            }
        }
        headBarBinding?.let {
            it.headTitle.setTextColor(ContextCompat.getColor(act,R.color.text_default))
            it.headTitle.text = title

            it.headMenu.text = ""
            it.headBack.setImageResource(R.drawable.icon_back_default)
            it.headBack.setOnClickListener {
                backAction.invoke()
                if (isBackFinish){
                    act.finish()
                }
            }
            it.headLlInner.visibility = if (showInner) View.VISIBLE else View.GONE
        }
    }

    /**
     * 白色背景 返回箭头 灰色
     * @param act 页面
     */
    fun initWhiteTitle(act : Activity, title : String, isOPBar :Boolean = true,
                       showInner :Boolean = false, enableKeyboard:Boolean = false,
                       isBackFinish :Boolean = true, backAction: () -> Unit = {}){
        if (isOPBar)
            ImmersionBar.with(act)
                .statusBarColor(R.color.white)
                .statusBarDarkFont(true)
                .keyboardEnable(enableKeyboard)
                .titleBarMarginTop(this)
                .init()
        headBarBinding?.let {
            it.headLlInner.setBackgroundResource(R.drawable.bg_head_bar_inner_w)
            it.headLlBar.setBackgroundResource(R.drawable.bg_head_bar_w)
            //it.headRoot.setBackgroundResource(R.color.white)
            it.headTitle.text = title
            it.headTitle.setTextColor(ContextCompat.getColor(act,R.color.text_default))
            it.headBack.setImageResource(R.drawable.icon_back_default)

            it.headMenu.text = ""
            it.headBack.setOnClickListener {
                backAction.invoke()
                if (isBackFinish){
                    act.finish()
                }
            }
            it.headLlInner.visibility = if (showInner) View.VISIBLE else View.GONE
        }
    }

    fun setInnerResource(res:Int){
        headBarBinding?.headLlInner?.setBackgroundResource(res)
    }


    fun setInnerBgColor(colorId:Int){
        headBarBinding?.headLlInner?.setBackgroundColor(colorId)
    }

    /**
     * 设置左侧图标
     * @param res 资源
     */
    fun setBackIcon(res : Int): ImageView? {
        headBarBinding?.headBack?.setImageResource(res)
        return headBarBinding?.headBack
    }

    /**
     * 设置左侧图标
     * @param isVisi 是否可见
     */
    fun setBackUnable(isVisi:Boolean = false){
        headBarBinding?.headBack?.visibility = if (isVisi) VISIBLE else INVISIBLE
    }

    /**
     * 设置title
     * @param title 标题文本
     */
    fun setTitle(title : String){
        headBarBinding?.headTitle?.text = title
    }

    /**
     * 设置title size
     * @param textSize 文本字体SP
     */
    fun setTitleSize(textSize : Float){
        headBarBinding?.headTitle?.setTextSize(TypedValue.COMPLEX_UNIT_SP,textSize)
    }

    /**
     * 设置右边文本
     * @param rightTx 右边文本
     */
    fun setRightText(rightTx:String,colorId: Int,action: () -> Unit = {}){
        headBarBinding?.headMenu?.run {
            this.text = rightTx
            this.setTextColor(getViewColor(this.context,colorId))
            this.setOnClickListener {
                action.invoke()
            }
        }
    }

    /**
     * 设置右边是否可点击
     * @param colorId 颜色资源
     */
    fun setRightEnable(colorId: Int,isEnable:Boolean){
        headBarBinding?.headMenu?.run {
            this.setTextColor(getViewColor(this.context,colorId))
            this.isEnabled = isEnable
        }
    }

}